﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperMap.Data;
using DTGroup.Networking.Framework.Utils;
using DTGroup.iNek.WinUI._Codes;

namespace DTGroup.iNek.WinUI._Codes
{
    public class ProjectionManager :DTGroup.Networking.Framework.Singletonable<ProjectionManager>
    {
        /// <summary>
        /// 锁
        /// </summary>
        private object locker = new object();
        
        /// <summary>
        /// 系统地图实体
        /// </summary>
        private List<ProjectionEntity> projectionCollection = null;

        public List<ProjectionEntity> ProjectionCollection
        {
            get
            {
                lock (locker)
                {
                    if (projectionCollection == null)
                    {

                        projectionCollection = ProjectionManager.Instance.GetAll();

                        // 当地图信息不存在时，避免返回NULL
                        if (projectionCollection == null)
                        {
                            projectionCollection = new List<ProjectionEntity>();
                        }
                    }

                    return projectionCollection;
                }
            }
        }

        /// <summary>
        /// 获取 所有的投影类型时
        /// </summary>
        /// <returns></returns>
        public List<ProjectionEntity> GetAll()
        {
            List<ProjectionEntity> projections = new List<ProjectionEntity>();

            Type type = typeof(SuperMap.Data.ProjectionType);
            System.Reflection.FieldInfo[] fieldInfos = type.GetFields();

            if (fieldInfos.Length > 0)
            {
                int cursor = 0;

                foreach (System.Reflection.FieldInfo item in fieldInfos)
                {
                    // 投影名称
                    string projectionTypeName = item.Name;

                    ProjectionType projectionType = StringUtil.TryParse<ProjectionType>(item.Name);
                    if (projectionTypeName.IndexOf("value__") == -1)
                    {
                        if (projectionType == ProjectionType.TransverseMercator)
                        {
                            projectionTypeName = "UTM";
                        }

                        using (Projection projection = new Projection(projectionType))
                        {
                            projections.Add(new ProjectionEntity() { OID = cursor, Name = projectionTypeName, ProjectionType = projection.Name });
                        };
                    }

                    cursor++;
                }
            }

            return projections;
        }
    }
}
